package net.minecraft.src;

import net.minecraft.client.Minecraft;

public class EntityPlayerPPH
{

	public EntityPlayerPPH(Minecraft minecraft)
	{
		this.minecraft = minecraft;
		init(player);
		inportal = false;
		timeUntilPortal = 10;
	}

	public boolean init(Minecraft mc)
	{
		return init(mc.thePlayer);
	}

	public boolean init(EntityPlayerSP player)
	{
		this.player = player;
		if (this.player == null)
		{
			System.err.println("WARNING: Player is null!");
			return false;
		}
		return true;
	}

	public void onTick(float f, Minecraft minecraft, boolean onGUI)
	{
		if (!init(minecraft))
			return;
		updateCounters();
		updatePortal();
	}

	private void updateCounters()
	{
		if (tickCounter + 1 > Integer.MAX_VALUE)
			tickCounter = -1;
		tickCounter++;
	}

	private void updatePortal()
	{
		if (inportal && tickCount(10) == 0)
		{
			if (!inPortalBlocks())
			{
				System.out.println("Left portal!");
				inportal = false;
				return;
			}
			if (timeUntilPortal-- <= 0)
			{
				inportal = false;
				if (player.dimension != 0 && player.dimension != -2)
				{
					System.out.println("The portal only works in our world and the normal world.");
					return;
				}
				swapWorld();
			}
		}
	}

	private void swapWorld()
	{
		System.out.println("Swapping Worlds!");
		player.worldObj.setEntityDead(player);
		player.isDead = false;
		double d = player.posX;
		double d1 = player.posZ;
		double d2 = 8D;
		if (player.dimension == 0) // Normal
		{
			d /= d2;
			d1 /= d2;
			player.setLocationAndAngles(d, player.posY, d1, player.rotationYaw, player.rotationPitch);
			if (player.isEntityAlive())
			{
				player.worldObj.updateEntityWithOptionalForce(player, false);
			}
			World world = null;
			world = new World(player.worldObj, WorldProvider.getProviderForDimension(-1));
			minecraft.changeWorld(world, "Entering the Nether", player);
		}
		else //PPH
		{
			d *= d2;
			d1 *= d2;
			player.setLocationAndAngles(d, player.posY, d1, player.rotationYaw, player.rotationPitch);
			if (player.isEntityAlive())
			{
				player.worldObj.updateEntityWithOptionalForce(player, false);
			}
			World world1 = null;
			world1 = new World(player.worldObj, WorldProvider.getProviderForDimension(0));
			minecraft.changeWorld(world1, "Leaving the Nether", player);
		}
		player.worldObj = player.worldObj;
		if (player.isEntityAlive())
		{
			player.setLocationAndAngles(d, player.posY, d1, player.rotationYaw, player.rotationPitch);
			player.worldObj.updateEntityWithOptionalForce(player, false);
			(new Teleporter()).placeInPortal(player.worldObj, player);
		}
	}

	public void setInPortal()
	{
		inportal = true;
		timeUntilPortal = 10;
	}

	private boolean inPortalBlocks()
	{
		int x = (int) Math.floor(player.posX);
		int y = (int) Math.floor(player.posY);
		int z = (int) Math.floor(player.posZ);
		return (player.worldObj.getBlockId(x, y, z) == mod_pph.portalBlock.blockID && player.worldObj.getBlockId(x, y - 1, z) == mod_pph.portalBlock.blockID) ? true : false;
	}

	public boolean inPortal()
	{
		return inportal;
	}

	public int tickCount(int id)
	{
		return tickCounter % id;
	}

	private boolean inportal;
	private int timeUntilPortal;
	private int tickCounter;

	private EntityPlayerSP player;
	private Minecraft minecraft;
}
